package com.winit.config.repository;

import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Update;
import com.winit.config.model.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

@Repository
public class ConfigRepositoryImpl implements ConfigRepositoryExt {

    @Autowired
    private CassandraOperations cassandraOperations;

    @Override
    public int updateExistPropertyById(Config config) {
        Update update = QueryBuilder.update("config");
//        update.setConsistencyLevel(ConsistencyLevel.ONE);
        update.where(QueryBuilder.eq("id", config.getId()));
        if (!StringUtils.isEmpty(config.getParentId())){
            update.with(QueryBuilder.set("parent_id", config.getParentId()));
        }
        if (null != config.getKVMap()){
            update.with(QueryBuilder.set("k_v_map", config.getKVMap()));
        }
        if (null != config.getKChangeMap()){
            update.with(QueryBuilder.set("k_change_map", config.getKChangeMap()));
        }
        if (null != config.getKRefMap()){
            update.with(QueryBuilder.set("k_ref_map", config.getKRefMap()));
        }
        cassandraOperations.execute(update);
        return 1;
    }
}
